#pragma once 
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys//types.h>
#include <arpa/inet.h>

class InetAddr
{
public:
    InetAddr(uint16_t port)
        :_port(port)
    {
        //服务器初始化
        _inet_addr.sin_family = AF_INET;
        _inet_addr.sin_port = _port;
        _inet_addr.sin_addr.s_addr = INADDR_ANY;
    }
    InetAddr(const InetAddr& self)
    {
        _inet_addr = self._inet_addr;
        _ip = self._ip;
        _port = self._port;
    }    
    InetAddr& operator=(const InetAddr& self){
        _inet_addr = self._inet_addr;
        _ip = self._ip;
        _port = self._port;
        return *this;
    }
    void Net2HostPort(){
        _port = ::ntohs(_port);
    }
    void Net2HostIp(){
        char Ip[64];
        if (::inet_ntop(AF_INET,&_inet_addr.sin_addr,Ip,sizeof(Ip)) == nullptr){
            _ip = "invalid ip";
        }
        else {
            _ip = Ip;
        }
    }
    //只有当我们给别人发送信息的时候, 才会需要这个初始化
    InetAddr(sockaddr_in& inet_addr)
        :_inet_addr(inet_addr)
    {
        Net2HostPort();
        Net2HostIp();
    }   
    //ip:port 
    string NetAddr() const {
        return _ip + ":" + to_string(_port);
    }
    socklen_t Len(){
        return sizeof(_inet_addr);
    }
    sockaddr* Addr(){
        return (sockaddr*)(&_inet_addr);
    }
    ~InetAddr(){

    }
private:
    sockaddr_in _inet_addr; //网络地址
    string _ip;
    uint16_t _port;
};